package com.example.demo.service;

// import org.flowable.dmn.api.DmnDecisionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * DMN 决策服务
 * 提供各种业务决策的 DMN 规则执行
 */
@Service
public class DmnDecisionService {

    @Autowired
    private org.flowable.dmn.api.DmnDecisionService flowableDmnService;

    /**
     * 执行请假审批决策
     */
    public Map<String, Object> executeLeaveApprovalDecision(String employeeLevel, Integer days, String leaveType) {
        Map<String, Object> decisionInput = new HashMap<>();
        decisionInput.put("employeeLevel", employeeLevel);
        decisionInput.put("days", days);
        decisionInput.put("leaveType", leaveType);

        try {
            var decisionResult = flowableDmnService.createExecuteDecisionBuilder()
                    .decisionKey("leaveApprovalDecision")
                    .variables(decisionInput)
                    .executeWithSingleResult();

            if (decisionResult != null) {
                Map<String, Object> result = new HashMap<>();
                result.put("approvalResult", decisionResult.get("approvalResult"));
                result.put("approverLevel", decisionResult.get("approverLevel"));
                result.put("needHRApproval", decisionResult.get("needHRApproval"));
                result.put("success", true);
                return result;
            } else {
                return createDefaultLeaveResult();
            }
        } catch (Exception e) {
            System.err.println("请假审批决策执行失败：" + e.getMessage());
            return createDefaultLeaveResult();
        }
    }

    /**
     * 执行风险评估决策
     */
    public Map<String, Object> executeRiskAssessmentDecision(String projectType, Integer budget, 
                                                           Integer teamSize, String experience) {
        Map<String, Object> decisionInput = new HashMap<>();
        decisionInput.put("projectType", projectType);
        decisionInput.put("budget", budget);
        decisionInput.put("teamSize", teamSize);
        decisionInput.put("experience", experience);

        try {
            var decisionResult = flowableDmnService.createExecuteDecisionBuilder()
                    .decisionKey("riskAssessmentDecision")
                    .variables(decisionInput)
                    .executeWithSingleResult();

            if (decisionResult != null) {
                Map<String, Object> result = new HashMap<>();
                result.put("riskLevel", decisionResult.get("riskLevel"));
                result.put("recommendedAction", decisionResult.get("recommendedAction"));
                result.put("requiredApproval", decisionResult.get("requiredApproval"));
                result.put("success", true);
                return result;
            } else {
                return createDefaultRiskResult();
            }
        } catch (Exception e) {
            System.err.println("风险评估决策执行失败：" + e.getMessage());
            return createDefaultRiskResult();
        }
    }

    /**
     * 执行折扣计算决策
     */
    public Map<String, Object> executeDiscountCalculationDecision(String customerType, Double orderAmount, 
                                                                Integer loyaltyPoints, String season) {
        Map<String, Object> decisionInput = new HashMap<>();
        decisionInput.put("customerType", customerType);
        decisionInput.put("orderAmount", orderAmount);
        decisionInput.put("loyaltyPoints", loyaltyPoints);
        decisionInput.put("season", season);

        try {
            var decisionResult = flowableDmnService.createExecuteDecisionBuilder()
                    .decisionKey("discountCalculationDecision")
                    .variables(decisionInput)
                    .executeWithSingleResult();

            if (decisionResult != null) {
                Map<String, Object> result = new HashMap<>();
                result.put("discountRate", decisionResult.get("discountRate"));
                result.put("finalAmount", decisionResult.get("finalAmount"));
                result.put("bonusPoints", decisionResult.get("bonusPoints"));
                result.put("success", true);
                return result;
            } else {
                return createDefaultDiscountResult();
            }
        } catch (Exception e) {
            System.err.println("折扣计算决策执行失败：" + e.getMessage());
            return createDefaultDiscountResult();
        }
    }

    /**
     * 获取所有可用的决策表
     */
    public Map<String, Object> getAvailableDecisions() {
        Map<String, Object> decisions = new HashMap<>();
        decisions.put("leaveApprovalDecision", "请假审批决策");
        decisions.put("riskAssessmentDecision", "风险评估决策");
        decisions.put("discountCalculationDecision", "折扣计算决策");
        return decisions;
    }

    /**
     * 创建默认的请假审批结果
     */
    private Map<String, Object> createDefaultLeaveResult() {
        Map<String, Object> result = new HashMap<>();
        result.put("approvalResult", "需要审批");
        result.put("approverLevel", "直接主管");
        result.put("needHRApproval", false);
        result.put("success", false);
        result.put("message", "未找到匹配的决策规则");
        return result;
    }

    /**
     * 创建默认的风险评估结果
     */
    private Map<String, Object> createDefaultRiskResult() {
        Map<String, Object> result = new HashMap<>();
        result.put("riskLevel", "中等");
        result.put("recommendedAction", "需要进一步评估");
        result.put("requiredApproval", "部门经理");
        result.put("success", false);
        result.put("message", "未找到匹配的决策规则");
        return result;
    }

    /**
     * 创建默认的折扣计算结果
     */
    private Map<String, Object> createDefaultDiscountResult() {
        Map<String, Object> result = new HashMap<>();
        result.put("discountRate", 0.0);
        result.put("finalAmount", 0.0);
        result.put("bonusPoints", 0);
        result.put("success", false);
        result.put("message", "未找到匹配的决策规则");
        return result;
    }
}
